package com.threeti.mecool.core.infrastructure.persistence.plan;

import com.threeti.mecool.core.domain.model.plan.pojo.TaskActionDataType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

public interface TaskActionDataTypeRepository extends JpaRepository<TaskActionDataType, Long> {
    List<TaskActionDataType> findByActionId(Integer actionId);

    @Query("select t from TaskActionDataType t where (removed is null or removed=0) and actionId=? and name=? order by id desc")
    List<TaskActionDataType> findByActionIdAndName(int actionId, String name);

    @Query("select t from TaskActionDataType t where (removed is null or removed=0) and code=?")
    TaskActionDataType findByCode(String code);

    List<TaskActionDataType> findByCodeIn(String... codes);

//    List<TaskActionDataType> findByActionIdAndNameIn(int actionId, String... names);

    @Query("select t from TaskActionDataType t where removed is null or removed=0 order by actionId, name, nvl(updatedAt, to_date('2000-1-1', 'yyyy-MM-dd')) desc")
    List<TaskActionDataType> findAllActive();

    @Query("select t from TaskActionDataType t where (removed is null or removed=0) and actionId=? order by name")
    List<TaskActionDataType> findActiveByActionId(Integer actionId);

    @Query("select t from TaskActionDataType t where (removed is null or removed=0) and actionId=? and updatedAt>=? and updatedAt<? order by updatedAt desc")
    List<TaskActionDataType> findActiveByActionIdAndUpdatedBetweenCloseOpen(Integer actionId, Date dateFrom, Date dateTo);
}
